package il.ac.braude.sona.control;

import il.ac.braude.sona.constants.ButtonPermission;
import il.ac.braude.sona.constants.Mssg;
import il.ac.braude.sona.constants.P;
import il.ac.braude.sona.constants.Resources;
import il.ac.braude.sona.entities.ChangeRequest;
import il.ac.braude.sona.gui.forms.FormFrame;
import il.ac.braude.sona.gui.forms.FormListener;
import il.ac.braude.sona.gui.forms.myforms.SurveyReportForm;
import il.ac.braude.sona.gui.infogui.RequsetInfoFrame;
import il.ac.braude.sona.ocsf.client.Client;
import il.ac.braude.sona.ocsf.client.ServerResponseListener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;
/**
 * The SurveyReportController handling all methods related to survey reports
 * @author Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 */
public class SurveyReportController implements  FormListener,ServerResponseListener {
	/**
	 * a form for creating a survey report 
	 */
	private SurveyReportForm createSurveyForm;
	/**
	 * form for viewing the survey report
	 */
	private SurveyReportForm viewSurveyForm;
	/**
	 * form for editing an exiting survey report
	 */
	private SurveyReportForm editSurveyReport;
	/**
	 * the request whom the survey report is written to
	 */
	private ChangeRequest myReq;
	/**
	 * frame for the change request
	 */
	private RequsetInfoFrame  reqFram;	

	/**
	 * creates and shows the new survey report frame,
	 * waiting for input from the surveyor
	 * @param req -change request to be surveyed 
	 * @param frame - that request frame
	 */
	public void execCreateSurveyReport(ChangeRequest req,RequsetInfoFrame frame) {
		if (createSurveyForm==null) {
			myReq=req;
			reqFram=frame;
			createSurveyForm  = new SurveyReportForm(req);
			createSurveyForm.addFormListener(SurveyReportController.this);
			createSurveyForm.showFrame();
		}
		else createSurveyForm.toFront();
	}
	/**
	 * opens the survey report and enables editing values
	 * creates the form from a given list
	 * @param list - values for this survey report
	 */
	public void editSurveyReport(ArrayList<String> list) {
		if (editSurveyReport==null) {
			editSurveyReport  = new SurveyReportForm(list);
			editSurveyReport.addFormListener(SurveyReportController.this);
			editSurveyReport.showFrame();
		}
		else editSurveyReport.toFront();
	}
     /**
      * creates a survey report form form the given values list 
      * in a read only mode for board review.
      * decisions made by the board will be handled in another method
      * @param list
      */
	private void viewSurveyReport(ArrayList<String> list) {
		if (viewSurveyForm==null) {
			viewSurveyForm  = new SurveyReportForm(list,new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					viewSurveyForm.SurveyReportAdditionalData.setVisible(true);
					viewSurveyForm.additionalDataButton.disapearButton();
					viewSurveyForm.setLocation(viewSurveyForm.getX(), 40);
					viewSurveyForm.pack();
				}});
			viewSurveyForm.addFormListener(SurveyReportController.this);
			viewSurveyForm.showFrame();
		}
		else viewSurveyForm.toFront();
	}
	
	//Callbacks//
	
	@Override
	public void onFormCanceled(FormFrame formFrame) {
		if (formFrame==createSurveyForm) {
			createSurveyForm.dispose();
			createSurveyForm=null;
		}
		if (formFrame==viewSurveyForm) {
			viewSurveyForm.dispose();
			viewSurveyForm=null;
		}
		if (formFrame==editSurveyReport) {
			editSurveyReport.dispose();
			editSurveyReport=null;
		}
	}
	
	@Override
	public void onFormSubmited(FormFrame formFrame, ArrayList<Object> values) {
		if (formFrame==createSurveyForm){
			if (values.contains("")){
				createSurveyForm.showMessege("ALL FIELDS MUST BE FILLED!");
				values.clear();
			}
			else{
		try {
			values.add(myReq.getID());
			values.add(myReq.getValue("Information System"));
			Mssg msg = new Mssg(P.createSurveyReport,values);
			Client.getInstance().sendToServer(this, msg);
		} catch (Exception e) {
			e.printStackTrace();
			createSurveyForm.showMessege("Something went wrong with the database.");
			createSurveyForm.dispose();
			createSurveyForm=null;
		}
			}
		}
		else if (formFrame==viewSurveyForm){
				if (!((String)values.get(5)).equals("")){
			try {
				ArrayList<String> addval=new  ArrayList<String>();
				addval.add(myReq.getID());
				addval.add((String)values.get(5));
				Mssg msg = new Mssg(P.AddAdditionalData,addval);
				Client.getInstance().sendToServer(new ServerResponseListener() {
					@Override
					public void onServerResponse(Mssg mssg) {
						if (mssg.is(P.error)) {
							JOptionPane.showMessageDialog(viewSurveyForm,"cant get time from DB");
							return;
						}
						else{ JOptionPane.showMessageDialog(viewSurveyForm,Resources.Strings.SurveyReportAdditionalData + " Updated");
						viewSurveyForm.dispose();
						viewSurveyForm.dispose();
						reqFram.dispose();
						}
					}
				}, msg);
			} catch (Exception e) {
				e.printStackTrace();
				viewSurveyForm.showMessege("Something went wrong with the database.");
				viewSurveyForm.dispose();
				viewSurveyForm=null;
			}
				}
				else {
					viewSurveyForm.dispose();
					viewSurveyForm=null;
				}
		}
		else if (formFrame==editSurveyReport){
			if (values.contains("")){
				editSurveyReport.showMessege("ALL FIELDS MUST BE FILLED!");
				values.clear();
			}
			else{
		try {
			values.add(myReq.getID());
			Mssg msg = new Mssg(ButtonPermission.EditSurveryReport,values);
			Client.getInstance().sendToServer(new ServerResponseListener() {
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.is(P.error)) {
						editSurveyReport.showMessege((String) mssg.getMessege());
						return;
					}
					else {
							JOptionPane.showMessageDialog(editSurveyReport,Resources.Strings.SurveyReportSaved);
							editSurveyReport.dispose();
							editSurveyReport.dispose();
							reqFram.dispose();
							
					}
				}
			}, msg);
		} catch (Exception e) {
			e.printStackTrace();
			createSurveyForm.showMessege("Something went wrong with the database.");
			createSurveyForm.dispose();
			createSurveyForm=null;
		}
			}
		}
	}
	
	

	@Override
	public void onServerResponse(Mssg mssg) {
		if (mssg.is(P.error)) {
			createSurveyForm.showMessege((String) mssg.getMessege());
			return;
		}
		else {
				JOptionPane.showMessageDialog(createSurveyForm,Resources.Strings.SurveyReportSaved);
				createSurveyForm.dispose();
				createSurveyForm.dispose();
				reqFram.dispose();
				
		}
	}
/**
 * method for creating a survey report in read only mode,sends a request to the server
 * and gets the values from the server.
 * opens a new view survey report form
 * @param req - request whom the survey report is attached to
 * @param requsetInfoFrame - frame for that survey report
 */
	public void execViewSurveyReport(ChangeRequest req, RequsetInfoFrame requsetInfoFrame) {
		reqFram=requsetInfoFrame;
		myReq=req;
		try {
			Mssg mssg = new Mssg(P.ViewMssg.view_survey_report_fo_rid, req.getID());
			Client.getInstance().sendToServer(new ServerResponseListener() {

				@SuppressWarnings("unchecked")
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.is(P.error)) {
						JOptionPane.showMessageDialog(null,mssg.getMessege());
						return;
					}
					else SurveyReportController.this.viewSurveyReport((ArrayList<String>)mssg.getMessege());
				}
			}, mssg);

		} catch (Exception e) {
			e.printStackTrace();
		}		
	}

	/**
	 * method for creating a survey report in editable mode,sends a request to the server
	 * and gets the values from the server.
	 * opens a new edit survey report form
	 * @param req - request whom the survey report is attached to
	 * @param requsetInfoFrame - frame for that survey report
	 */
	public void execEditSurveyReport(ChangeRequest req,	RequsetInfoFrame requsetInfoFrame) {
			reqFram=requsetInfoFrame;
			myReq=req;
			try {
				Mssg mssg = new Mssg(P.ViewMssg.view_survey_report_fo_rid, req.getID());
				Client.getInstance().sendToServer(new ServerResponseListener() {

					@SuppressWarnings("unchecked")
					@Override
					public void onServerResponse(Mssg mssg) {
						if (mssg.is(P.error)) {
							JOptionPane.showMessageDialog(null,mssg.getMessege());
							return;
						}
						else SurveyReportController.this.editSurveyReport((ArrayList<String>)mssg.getMessege());
					}
				}, mssg);

			} catch (Exception e) {
				e.printStackTrace();
			}		
		
	}

}
